package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dto.EmpContractAddDTO;
import org.jsola.hr.dto.EmpContractUpdateDTO;
import org.jsola.hr.entity.EmpContractDO;
import org.jsola.hr.query.EmpContractQuery;
import org.jsola.hr.service.IEmpContractService;
import org.jsola.hr.vo.EmpContractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * 员工合同信息
 *
 * @author zhr
 */
@Component
public class EmpContractTestHelper extends BaseTestHelper {
    
    @Autowired
    private IEmpContractService empContractService;

    /**
     * 生成add测试数据
     * @return EmpContractAddDTO
     */
    public EmpContractAddDTO getEmpContractAddDTO(){
        EmpContractAddDTO empContractAddDTO = new EmpContractAddDTO();
        empContractAddDTO.setContractId(SafeKit.getLong(getRandomValue("bigint",11)));
        empContractAddDTO.setEmpId(getRandomValue("varchar",32));
        empContractAddDTO.setType(getRandomValue("varchar",100));
        empContractAddDTO.setStartTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        empContractAddDTO.setEndTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        empContractAddDTO.setName(getRandomValue("varchar",255));
        empContractAddDTO.setTermYear(SafeKit.getInteger(getRandomValue("tinyint",6)));
        empContractAddDTO.setTermMonth(SafeKit.getInteger(getRandomValue("tinyint",10)));
        empContractAddDTO.setTermDay(SafeKit.getInteger(getRandomValue("tinyint",10)));
        empContractAddDTO.setRemark(getRandomValue("text",2147483647));
        empContractAddDTO.setCompanyId(getRandomValue("varchar",32));
        return empContractAddDTO;
    }


    /**
     * 生成update测试数据
     * @return EmpContractUpdateDTO
     */
    public EmpContractUpdateDTO getEmpContractUpdateDTO(EmpContractVO empContractVO){
        return empContractService.selectDOById(empContractVO.getId(), SITE_ID).to(EmpContractUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return EmpContractQuery
     */
    public EmpContractQuery getEmpContractQuery() {
        EmpContractQuery empContractQuery = new EmpContractQuery();
        List<EmpContractDO> empContractDOList = empContractService.list(new EmpContractDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(empContractDOList)) {
            EmpContractDO empContractDO = empContractDOList.get(0);
            empContractQuery = empContractDO.to(EmpContractQuery.class);
            if (empContractDO.getStartTime() != null) {
                empContractQuery.setStartTimeStart(DateKit.getBeforeDay(empContractDO.getStartTime(),-1));
                empContractQuery.setStartTimeEnd(DateKit.getBeforeDay(empContractDO.getStartTime(),1));
            }
            if (empContractDO.getEndTime() != null) {
                empContractQuery.setEndTimeStart(DateKit.getBeforeDay(empContractDO.getEndTime(),-1));
                empContractQuery.setEndTimeEnd(DateKit.getBeforeDay(empContractDO.getEndTime(),1));
            }
        }
        return empContractQuery;
    }

    /**
     * 保存示例
     * @return EmpContractVO
     */
    public EmpContractVO saveEmpContractExample(){
        EmpContractAddDTO addDTO = this.getEmpContractAddDTO();
        EmpContractVO empContractVO = empContractService.save(addDTO,buildTokenUser());
        return empContractVO;
    }


}

